
$(document).ready(doInit);

function doInit() {
	initAppAdminInfos();
		
	initGroupApparatus();
	
	initDevices();
	
	$('#chooseAppGroup').click(function() {
		$('#win').window("open");
	
		$('#win').window({collapsible:false,
		    minimizable:false,
		    maximizable:false,
		    closable:false,
			closed:false,
			tools:[{
		        iconCls:'icon-cancel',
		        handler:function(){
		        	   $('#win').window("close");
		        }
	   		 }]
		});
	});
	
	
	$('#chooseDeviceNO').click(function() {
		$('#deviceWin').window("open");
	
		$('#deviceWin').window({collapsible:false,
		    minimizable:false,
		    maximizable:false,
		    closable:false,
			closed:false,
			tools:[{
		        iconCls:'icon-cancel',
		        handler:function(){
		        	 $('#deviceWin').window("close");
		        }
	   		 }]
		});
	});
	
	
	
	$('#chooseAppAdmin').click(function() {
		$('#appAdminWin').window("open");
	
		$('#appAdminWin').window({collapsible:false,
		    minimizable:false,
		    maximizable:false,
		    closable:false,
			closed:false,
			tools:[{
		        iconCls:'icon-cancel',
		        handler:function(){
		        	 $('#appAdminWin').window("close");
		        }
	   		 }]
		});
	});
	
}

function clearData() {
	$('input[type="text"]').val('');
	
	$('#introduction').val('');
	
	$('#showAppAdmin').text('');
	$('#appAdminNo').val('0');
	
	$('#showDeviceName').text('');
	$('#deviceNO').val('');
	
	$('#forAppGroupCode').text('');
	$('#appGroupCode').val('');
}

/**
 * 初始化仪器种类，显示仪器种类列表
 */
function initGroupApparatus() {
	$('#apparatusGroup').datagrid({
	    title:'',
	    iconCls:'icon-save',
	    singleSelect:true,
	    url:'apparatus-group!queryApparatusGroup.action',
	    idField: 'appGroupCode',
	    columns:[[
	        {field:'appGroupName',title:'种类名称',width:130},
	        {field:'remark',title:'说明',width:100},
	        {field:'isUsed',title:'是否使用',width:50,
	        	formatter : function(value, row, index) {
						if ('1' == value) {
							return '是';
				 		} else {
							var e = '否';
							return e;
						}
				}
	        },
	        {field:'appGroupCode',title:'选中',width:100,align:'center',
	        	    formatter : function(value, row, index) {
							return  '<a href="#" style="color:red;text-decoration:none;" onclick="choose('+ index + ',\'' + value +'\')">选中</a> ';
					}
			}
	    ]],
	     pagination:true,
	     pageList:[10],
	     rownumbers: true
	});
}

/**
 * 选择仪器种类
 */
function choose(index, value) {
	$('#apparatusGroup').datagrid('clearSelections');
	$('#apparatusGroup').datagrid('selectRow', index);
	
	var selected = $('#apparatusGroup').datagrid('getSelected');
	
	$('#forAppGroupCode').text(selected.appGroupName);
	$('#appGroupCode').val(selected.appGroupCode);
	
	$('#win').window('close');
}

/**
 * 初始化设备信息
 */
function initDevices() {
	$('#deviceInfos').datagrid({
	    title:'',
	    iconCls:'icon-save',
	    singleSelect:true,
	    url:'idevice!getIdevices.action',
	    idField: 'deviceID',
	    columns:[[
	        {field:'devName',title:'设备名称',width:100},
	        {field:'deviceID',title:'选中',width:100,align:'center',
	        	    formatter : function(value, row, index) {
							return  '<a href="#" style="color:red;text-decoration:none;" onclick="chooseDevices('+ index + ',\'' + value +'\')">选中</a> ';
					}
			}
	    ]],
	     pagination:true,
	     pageList:[10],
	     rownumbers: true,
	     fitColumns:true
	});
}

/**
 * 选择设备信息
 */
function chooseDevices(index, value) {
	$('#deviceInfos').datagrid('clearSelections');
	$('#deviceInfos').datagrid('selectRow', index);
	
	var selected = $('#deviceInfos').datagrid('getSelected');
	
	$('#showDeviceName').text(selected.devName);//devName
	$('#deviceID').val(selected.deviceID);
	
	$('#deviceWin').window('close');
}



/**
 * 初始化仪器管理员信息
 */
function initAppAdminInfos() {
	$('#appAdminInfos').datagrid({
	    title:'',
	    iconCls:'icon-save',
	    singleSelect:true,
	    url:'iemployee!queryForApparatusAdmin.action',
	    idField: 'deviceID',
	    columns:[[
	        {field:'empName',title:'真实姓名',width:fixWidth(0.1)},
	        {field:'loginAccount',title:'登录名',width:fixWidth(0.1)},
	        {field:'linkPhone',title:'联系电话',width:fixWidth(0.1)},
	        {field:'empType', title:'用户类型', width:fixWidth(0.1),
	        	formatter:function(value, rowData, rowIndex) {
						var content ='';
						if ('1' == value) {
							content = '注册用户';
						} else if ('2' == value) {
							content = '正式用户';
						} else if ('3' == value) {
							content = '课题组长';
						} else if ('4' == value) {
							content = '仪器管理员';
						} else if ('5' == value) {
							content = '实验室管理员';
						} else if ('6' == value) {
							content = '系统管理员';
						} else {
							content = '';
						}
						
						return content;
					}
	        },
	        {field:'userType',title:'身份',width:fixWidth(0.03),
	        	formatter : function(value, rowData, rowIndex) {//0:院内、1:校内、2:校外
	        	    	var x = '<a href="#" onclick="showDetail('+ rowIndex + ',\'' + value +'\')">详情&nbsp;&nbsp;</a> ';
	        	    	
						if (value == '0') {
							return '院内';
				 		} else if (value == '1'){
				 			return '校内';
						} else if (value == '2') {
							return '校外';
						}
					}
	        },
	        {field:'empID',title:'选中',width:100,align:'center',
	        	    formatter : function(value, row, index) {
							return  '<a href="#" style="color:red;text-decoration:none;" onclick="chooseAppAdminWin('+ index + ',\'' + value +'\')">选中</a> ';
					}
			}
	    ]],
	     pagination:true,
	     pageList:[10],
	     rownumbers: true,
	     fitColumns:true
	});
}

/**
 * 选择仪器管理员信息
 */
function chooseAppAdminWin(index, value) {
	$('#appAdminInfos').datagrid('clearSelections');
	$('#appAdminInfos').datagrid('selectRow', index);
	
	var selected = $('#appAdminInfos').datagrid('getSelected');
	
	$('#showAppAdmin').text(selected.empName);
	$('#appAdminNo').val(selected.empID);
	
	$('#appAdminWin').window('close');
}